package com.cskaoyan.order.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dto.CancelOrderRequest;
import com.cskaoyan.order.dto.CancelOrderResponse;
import com.cskaoyan.order.dto.DeleteOrderRequest;
import com.cskaoyan.order.dto.DeleteOrderResponse;
import com.cskaoyan.order.service.OrderCoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class OrderCoreController {
    @Autowired
    OrderCoreService orderCoreService;


    @DeleteMapping("/shopping/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id") String id) {

        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        DeleteOrderResponse response = orderCoreService.deleteOrder(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

  //取消订单
    @PostMapping("/shopping/cancelOrder")
    public ResponseData cancelOrder(@RequestBody Order order) {
        String orderId = order.getOrderId();
      //  String orderId = servletRequest.getParameter("orderId");
        CancelOrderRequest request = new CancelOrderRequest();
        request.setOrderId(orderId);

        // 调用service处理请求
        CancelOrderResponse response = orderCoreService.cancelOrder(request);

        if (OrderRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功，直接返回前端需要的数据
            return new ResponseUtil().setData(response);
        }
        // 调用失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
